require(['jquery',
         'global',
         'module/util',
         'module/ajax',
         'jquery.validate',
         'bootstrap',
         'requirejs/domready!'], 
function($, global, util){
	
	(function() {
		var content = $("#formPanel");
		var height = $(document.body).height(), width = $(document.body).width();
		var contentHeight = content.height(), contentWidth = content.width();
		$("#bodyPanel").css({
			height : height,
			'padding-top' : (height/2 - contentHeight/2),
			'padding-left' : ((width/2 - contentWidth/2))
		});
		content.show();
	})();
	
	$("#cancelBtn").click(function() {
		WeixinJSBridge.call('closeWindow');
	});
	
	
	// Add event handler for captcha image click
	/*$("#captcha-img").click(function() {
	    $(this).attr("src", global.context+ "/web/auth/captcha?r=" + Math.random());
	});*/
	
	// Add jQuery validate to sign-in form
	$('#signin-form').validate({
		onkeyup : false,
		rules:{
			username:{
				required: true,
				rangelength: [11,11]
            },
            password:{
				required: true,
				rangelength: [8,30]
		    }
		    /*,captcha:{
				required: true,
				minlength:[5], 
				maxlength: [5]
		    }*/
	    },
	    messages : {
	    	username:{
	    		 required: "用户名不能为空",
	    		 rangelength : $.format("用户名长度：{0}-{1}个字符")
	        },
	        password:{
		  		required: "密码不能为空",
		  		rangelength : $.format("密码长度：{0}-{1}个字符")
		    }
	        /*,captcha:{
		  		required: "验证码不能为空",
		  		rangelength : $.format("验证码为5个字符")
		    }*/
	    },submitHandler: function(form) {
	    	$(form).find("input[type=submit]").val("正在登陆...").attr("disabled", true);
			util.ajax_submit(form).complete(function(){
				$('#captcha-img').attr("src", global.context+ "/web/auth/captcha?r=" + Math.random());
				$(form).find("input[type=submit]").val("登陆").attr("disabled", false);
			});
	    },
	    highlight: function(element, errorClass, validClass) {
	    	var parent = $(element).parent().parent();
	    	$(element).addClass(errorClass).removeClass(validClass);
	    	parent.addClass('weui_cell_warn').removeClass('has-success').addClass('has-error').find('.fa').remove();
	    	parent.append('<i class="fa fa-exclamation-circle form-control-feedback"></i>');
	    },
	    unhighlight: function(element, errorClass, validClass) {
	    	var parent = $(element).parent().parent();
	    	$(element).removeClass(errorClass).addClass(validClass);
	    	parent.removeClass('weui_cell_warn').removeClass('has-error').addClass('has-success').find('.fa').remove();
	    	parent.append('<i class="fa fa-check form-control-feedback"></i>');
	    }
	});
});
